<?php

namespace App\Game;

use App\Game\Game;
use App\Models\Log\PlayerBetFlow;
use Carbon\Carbon;

class qt extends Game
{
    private $platName = 'qt';


    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }

    public function getToken()
    {
        $arr = array(
            'grant_type' => 'password',
            'response_type' => 'token',
            'username' => $this->username,
            'password' => $this->password
        );
        $url = $this->gameUrl . '/v1/auth/token?' . http_build_query($arr);
        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['access_token'])) {
            return $outputarr['access_token'];
        } else {
            return false;
        }
    }

    //创建用户

    public function curlCreateMember($accountUserName, $password, $request)
    {
        $token = $this->getToken();
        $this->header = [
            "Authorization" => "Bearer " . $token, "Content-Type" => "application/json"
        ];
        $url = $this->gameUrl . '/v1/games/' . $this->gameCode . '/launch-url';
        $data = [
            'json' => [
                'playerId' => $accountUserName,
                'currency' => 'CNY',
                'country' => 'CN',
                'gender' => 'M',
                'birthDate' => '1986-01-01',
                'lang' => 'zh_CN',
                'mode' => 'real',
                'returnUrl' => 'http://www.ybgj.vip',
                'device' => 'mobile',
                'betLimitCode' => 1
            ]
        ];
        $output = $this->request('POST', $url, [], $this->header, $data);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['url'])) {
            $this->request('GET', $outputarr['url']);
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建失败'];
        }
    }


    //获取余额

    public function curlGetBalance($accountUserName)
    {
        $token = $this->getToken();
        $this->header = [
            "Authorization" => "Bearer " . $token, "Content-Type" => "application/json"
        ];
        $url = $this->gameUrl . '/v1/wallet/ext/' . $accountUserName;

        $output = $this->request('GET', $url, [], $this->header);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['currency'])) {
            return ['success' => true, 'data' => ['balance' => $outputarr['amount']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '用户不存在'];
        }
    }

    //转入游戏

    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $token = $this->getToken();
        $this->header = [
            "Authorization" => "Bearer " . $token, "Content-Type" => "application/json"
        ];
        $url = $this->gameUrl . '/v1/fund-transfers';

        $data = [
            'json' => [
                'type' => 'CREDIT',
                'referenceId' => $transferid,
                'playerId' => $accountUserName,
                'amount' => intval($price),
                'currency' => 'CNY'
            ]
        ];

        $output = $this->request('POST', $url, [], $this->header, $data);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['status']) && $outputarr['status'] == 'PENDING') {
            $url = $this->gameUrl . '/v1/fund-transfers/' . $outputarr['id'] . '/status';
            $data = [
                'json' => [
                    "status" => "COMPLETED"
                ]
            ];
            $output = $this->request('PUT', $url, [], $this->header, $data);
            $outputarr = json_decode($output, true);

            if ($outputarr['status'] == 'COMPLETED') {
                return ['success' => true, 'data' => ['transferid' => $outputarr['id'],'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //转出游戏

    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $token = $this->getToken();
        $this->header = [
            "Authorization" => "Bearer " . $token, "Content-Type" => "application/json"
        ];
        $url = $this->gameUrl . '/v1/fund-transfers';

        $data = [
            'json' => [
                'type' => 'DEBIT',
                'referenceId' => $transferid,
                'playerId' => $accountUserName,
                'amount' => intval($price),
                'currency' => 'CNY'
            ]
        ];

        $output = $this->request('POST', $url, [], $this->header, $data);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['status']) && $outputarr['status'] == 'PENDING') {
            $url = $this->gameUrl . '/v1/fund-transfers/' . $outputarr['id'] . '/status';
            $data = [
                'json' => ["status" => "COMPLETED"]
            ];
            $output = $this->request('PUT', $url, [], $this->header, $data);
            $outputarr = json_decode($output, true);

            if ($outputarr['status'] == 'COMPLETED') {
                return ['success' => true, 'data' => ['transferid' => $outputarr['id'],'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //查询订单状态

    public function curlCheckTransfer($request)
    {
        $token = $this->getToken();
        $this->header = [
            "Authorization" => "Bearer " . $token, "Content-Type" => "application/json"
        ];
        $url = $this->gameUrl . '/v1/fund-transfers/' . $request->transferid;

        $output = $this->request('GET', $url, [], $this->header);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['referenceId'])) {
            if($outputarr['status']=='COMPLETED'){
                return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
            }elseif($outputarr['status']=='CANCELLED'){
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败'];
            }else{
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转账失败,待定'];
            }

        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '用户不存在'];
        }
    }

    //踢线

    public function curlKick($request)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有踢线功能'];
    }

    //进入游戏

    public function curlJoinGame($request)
    {
        $accountUserName = $request->input('accountUserName');
        $gamecode = $request->input('gamecode', '');
        $demo = $request->input('demo', '');

        $token = $this->getToken();
        $this->header = [
            "Authorization" => "Bearer " . $token, "Content-Type" => "application/json"
        ];
        $url = $this->gameUrl . '/v1/games/' . $gamecode . '/launch-url';
        $demo = empty($demo) ? 'real' : 'demo';
        $data = [
            'json' => [
                'playerId' => $accountUserName,
                'currency' => 'CNY',
                'country' => 'CN',
                'gender' => 'M',
                'birthDate' => '1986-01-01',
                'lang' => 'zh_CN',
                'returnUrl' => '',
                'device' => 'desktop',
                'betLimitCode' => 1,
                'mode' => $demo
            ]
        ];

        $output = $this->request('POST', $url, [], $this->header, $data);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['url'])) {
            return ['success' => true, 'data' => ['url' => $outputarr['url'], 'method' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }


    public function curlJoinMobileGame($request)
    {
        $accountUserName = $request->input('accountUserName');
        $gamecode = $request->input('gamecode', '');
        $demo = $request->input('demo', '');

        $token = $this->getToken();
        $this->header = [
            "Authorization" => "Bearer " . $token, "Content-Type" => "application/json"
        ];
        $url = $this->gameUrl . '/v1/games/' . $gamecode . '/launch-url';
        $demo = empty($demo) ? 'real' : 'demo';
        $data = [
            'json' => [
                'playerId' => $accountUserName,
                'currency' => 'CNY',
                'country' => 'CN',
                'gender' => 'M',
                'birthDate' => '1986-01-01',
                'lang' => 'zh_CN',
                'returnUrl' => 'http://www.ybgj.vip',
                'device' => 'mobile',
                'betLimitCode' => 1,
                'mode' => $demo
            ]
        ];

        $output = $this->request('POST', $url, [], $this->header, $data);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['url'])) {
            return ['success' => true, 'data' => ['url' => $outputarr['url'], 'method' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $page = 0;
        $size = 500;
        $start_time = $startTime->format('Y-m-d\TH:i:s');
        $end_time = $endTime->format('Y-m-d\TH:i:s');
        $token = $this->getToken();
        $this->header = [
            "Authorization" => "Bearer " . $token,
            "Content-Type" => "application/json"
        ];
        $url = $this->gameUrl . '/v1/game-rounds';

        do {
            $flag = false;
            $params = [
                'status' => 'COMPLETED',
                'from' => $start_time,
                'to' => $end_time,
                'size' => $size,
                'page' => $page
            ];
            $output = $this->request('GET', $url, $params, $this->header);
            $output = json_decode($output, true);
            if (isset($output['totalCount']) && count($output['items']) >= 1) {
                $data = array_merge($data, $output['items']);
                $flag = true;
            }
            $page++;
        } while ($flag);

        return $data;
    }
}